package com.ja.openchat.views;

import android.content.Context;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.google.gson.JsonObject;
import com.ja.openchat.R;
import com.lxj.xpopup.util.XPopupUtils;
import com.zy.openchat.core.manager.TelegramManager;
import com.zy.openchat.core.manager.TgResponse;
import com.zy.openchat.core.manager.TgResultBlock;
import com.zy.openchat.core.model.message.ChatInfo;

import org.apache.commons.lang3.StringUtils;

import java.util.Map;

import androidx.annotation.NonNull;
import me.goldze.mvvmhabit.utils.ToastUtils;

public class ChatGroupNickNameDialog extends BaseBottomDialog{

    private long chatId;
    private String groupName = "";



    public ChatGroupNickNameDialog(@NonNull Context context, long chatId, String groupName, OnItemClickListner listner) {
        super(context);
        this.chatId = chatId;
        this.groupName = groupName;
    }

    @Override
    protected int getImplLayoutId() {
        return R.layout.dialog_chat_group_nickname_view;
    }


    @Override
    protected void onCreate() {
        super.onCreate();

        findViewById(R.id.tv_cancel).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });
        TextView tv_save = findViewById(R.id.tv_save);
        EditText editText = findViewById(R.id.et_input);
        ImageView ivClear = findViewById(R.id.iv_clear);
        ivClear.setOnClickListener(v -> editText.setText(""));
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if(TextUtils.isEmpty(s.toString())){
                    ivClear.setVisibility(INVISIBLE);
                    tv_save.setBackgroundResource(R.drawable.login_btn_bg_green_90_unselect);

                }else {
                    ivClear.setVisibility(VISIBLE);
                    tv_save.setBackgroundResource(R.drawable.login_btn_bg_green_90);
                }
            }
        });
        editText.setText(groupName);
        editText.setSelection(groupName.length());

        tv_save.setOnClickListener(v -> {
            if(TextUtils.isEmpty(editText.toString().trim())){
                ToastUtils.showShort("请输入群昵称");
                return;
            }

            saveGroupName(editText.getText().toString().trim());
        });


    }

    private void saveGroupName(String name) {
        if (!StringUtils.isEmpty(name)) {
            showProgress();
            TelegramManager.getTelegramManager().setGroupName(getContext(), chatId, name, new TgResultBlock<TgResponse>() {
                @Override
                public void onResult(Map<String, Object> request, JsonObject response, TgResponse obj) {
                    hideProgress();
                    if (obj.ok) {//设置成功
                        dismiss();
                    } else {
                        showErrorTip(getContext().getString(R.string.request_error) + obj.errString);
                    }
                }

                @Override
                public void onTimeout(Map<String, Object> request) {
                    hideProgress();
                    showErrorTip(getContext().getString(R.string.request_error));
                }
            });
        } else {
            showErrorTip(getContext().getString(R.string.input_group_name));
        }
    }


    @Override
    protected void onShow() {
        super.onShow();
    }

    @Override
    protected void onDismiss() {
        super.onDismiss();
    }

    @Override
    protected int getMaxHeight() {
        return (int) (XPopupUtils.getAppHeight(getContext()) * 0.98f);
    }


    public interface OnItemClickListner {
        void clickPostion(int position,ChatInfo postion);
    }

}
